const express = require('express')
const app = express()
const fs = require('fs')
const path = require('path')
const PORT = 9080
const BASE_URL = '/api'

const mockDir = path.join(__dirname, './mock')

function readdirDone(error, files) {
  if (error) throw new Error(error)

  files.forEach(file => {
    const _httpObject = require(`${mockDir}/${file}`)
    createHttpMethod(_httpObject)
  })
  console.log('mock data is loaded.');
}

function createHttpMethod({method = 'get', url = '', reply}) {
  console.log(`load [${method}]${url}`);
  return app[method](`${BASE_URL}${url}`, reply) 
}

fs.readdir(mockDir, readdirDone)

app.listen(PORT, _ => {
  console.log(`server服务已经运行：http://127.0.0.1:${PORT}`)
})
